package cn.cdqf.web;

import cn.cdqf.bean.Statistics;
import cn.cdqf.bean.TEmp;
import cn.cdqf.bean.TLoginlog;
import cn.cdqf.common.ResultResponse;
import cn.cdqf.service.EmpService;
import cn.cdqf.service.LoginlogService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.List;

@Controller
public class EmpServlet {

    @Autowired
    private EmpService empService;

    @Autowired
    private LoginlogService loginlogService;

    @RequestMapping("/EmpLogin")
    @ResponseBody
    public String EmpLogin(String no, String pass,String ip,String location, HttpServletResponse response, HttpSession session) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        ResultResponse resultResponse = null;
        TEmp emp = empService.login(no, pass);
        if (emp == null){
            resultResponse = new ResultResponse(200,"账号密码错误!");
        }else if(emp != null){
            if (emp.getFlag() != 1){
                resultResponse = new ResultResponse(300,"用户已冻结！");
            }else if(emp.getDel() != 0){
                resultResponse = new ResultResponse(400,"用户已删除！");
            }else {
                session.setAttribute("emp",emp);
                resultResponse = new ResultResponse(100,"登录成功！");
                TLoginlog tLoginlog = new TLoginlog(ip,emp.getNo(),location);
                loginlogService.addLoginlog(tLoginlog);
            }
        }
        return JSON.toJSONString(resultResponse);
    }

    @RequestMapping("/addEmp")
    @ResponseBody
    public ResultResponse addEmp(TEmp tEmp){
        int i = empService.addEmp(tEmp);
        ResultResponse resultResponse = null;
        if (i> 0){
            resultResponse = new ResultResponse(100,"添加成功");
        }else {
            resultResponse = new ResultResponse(200,"添加失败");
        }
        return resultResponse;
    }

    @RequestMapping("/queryEmp")
    @ResponseBody
    public List<TEmp> queryEmp(){
        List<TEmp> tEmps = empService.queryEmp();
        return tEmps;
    }

    @RequestMapping("/deleteEmp")
    @ResponseBody
    public ResultResponse  deleteEmp(String id){
        int i = empService.deleteEmp(Integer.parseInt(id));
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"删除成功");
        }else {
            resultResponse = new ResultResponse(200,"删除失败");
        }
        return resultResponse;
    }

    @RequestMapping("/queryEmpById")
    @ResponseBody
    public TEmp queryEmpById(String id){
        TEmp tEmp = empService.queryEmpById(Integer.parseInt(id));
        return tEmp;
    }

    @ResponseBody
    @RequestMapping("/updateEmp/{id}")
    public ResultResponse updateEmp(TEmp tEmp, @PathVariable("id") Integer id){
        int i = empService.updateEmp(tEmp,id);
        ResultResponse resultResponse = null;
        if (i> 0){
            resultResponse = new ResultResponse(100,"更新成功");
        }else {
            resultResponse = new ResultResponse(200,"更新失败");
        }
        return resultResponse;
    }

    @RequestMapping("/updateEmpPass")
    @ResponseBody
    public ResultResponse updateEmpPass(String pass1, String pass2,HttpSession session){
        TEmp emp = (TEmp) session.getAttribute("emp");
        ResultResponse resultResponse = null;
        String pass = emp.getPass();
        long id = emp.getId();
        if (pass.equals(pass1)){
            if (!pass.equals(pass2)){
                int i = empService.updateEmpPass(pass2,id);
                if (i> 0){
                    resultResponse = new ResultResponse(100,"修改成功");
                    emp.setPass(pass2);
                    session.setAttribute("emp",emp);
                }else {
                    resultResponse = new ResultResponse(200,"修改失败");
                }
            }else {
                resultResponse = new ResultResponse(102,"新密码不能与原始密码相同");
            }
        }else {
            resultResponse = new ResultResponse(101,"原始密码错误！");
        }
        return resultResponse;
    }
}
